package cn.longg.controller;

import cn.longg.entity.*;
import cn.longg.service.IDeptService;
import cn.longg.service.IMeetingJoinService;
import cn.longg.service.IMeetingService;
import com.github.pagehelper.PageInfo;
import com.sun.xml.internal.ws.resources.HttpserverMessages;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RequestMapping("main/meeting")
@RestController
public class MeetingController {
    @Autowired
    private IMeetingService meetingService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IMeetingJoinService meetingJoinService;

    @RequestMapping("toMeeting")
    public ModelAndView toMeeting(){
        return new ModelAndView("meeting");
    }

    @RequestMapping("selectByPage/{page}/{pageSize}")
    public Result selectByPage(@PathVariable int page, @PathVariable int pageSize, @RequestBody Meeting meeting){
        PageInfo pageInfo = meetingService.selectByPage(page,pageSize,meeting);
        return new Result(true,"查询成功",pageInfo);
    }

    @RequestMapping("toMeetingDetail")
    public ModelAndView toMeetingDetail(){
        return new ModelAndView("meeting_detail");
    }

    @RequestMapping("toAddMeeting")
    public ModelAndView toAddMeeting(){
        return new ModelAndView("meeting_add");
    }

    @RequestMapping("addMeeting")
    public Result addMeeting(@RequestBody Meeting meeting){
        meeting.setStatus(0);
        Dept dept = deptService.selectByPrimaryKey(meeting.getDeptId());
        meeting.setDeptName(dept.getName());
        meeting.setPublishDate(new Date());
        int i = meetingService.insertSelective(meeting);
        return new Result(0 < i,"",null);
    }

    @RequestMapping("ifJoin")
    public Result ifJoin(HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        MeetingJoin meetingJoin = new MeetingJoin();
        meetingJoin.setuId(loginUser.getId());
        MeetingJoin meetingJoin1 = meetingJoinService.selectOne(meetingJoin);
        return new Result(true,"",null == meetingJoin1);

    }

    @RequestMapping("joinMeeting")
    public Result joinMeeting(@RequestBody Meeting meeting,HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        MeetingJoin meetingJoin = new MeetingJoin();
        meetingJoin.setuId(loginUser.getId());
        meetingJoin.setcId(meeting.getId());
        int i = meetingJoinService.insertSelective(meetingJoin);
        return new Result(0 < i,"",null);
    }

    @RequestMapping("outMeeting")
    public Result outMeeting(@RequestBody Meeting meeting,HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        MeetingJoin meetingJoin = new MeetingJoin();
        meetingJoin.setuId(loginUser.getId());
        meetingJoin.setcId(meeting.getId());
        int i = meetingJoinService.delete(meetingJoin);
        return new Result(0 < i,"",null);
    }

    @RequestMapping("checkStatus")
    public Result checkStatus(){
        meetingService.checkStatus();
        return new Result(true,"",null);
    }
}
